﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Data;
using System.Web.UI.WebControls;

using Microsoft.Practices.Unity;

using Wuqi.Webdiyer;

using DreamBuilder.UI.UserControls;
using Web.SystemConfig.WorkFlow;
using Web.SystemLogin.Models;
using DreamBuilder.Common;
using Web.SystemConfig.Services;
using ePlanet.ArchiveMange.Services;
using System.Collections;
using Web.SystemConfig.SysCache;
using DreamBuilder.Core.Container;
using CommonModule;

namespace Web
{
    /// <summary>
    /// 档案流程业务基类
    /// </summary>
    public class ArchiveFlowEditPageBase : PageBase
    {

        private ArchiveTableEnum _currArchiveKind;

        protected bool canView = true;

        protected bool canEdit = false;

        protected bool canOperate = false;

        protected bool showReturn = true;

        protected bool IsFromDataManage
        {
            get
            {
                if (Request.QueryString["IsFromDataManage"] == "true")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        protected string btnAddArchiveBackText
        {
            get
            {
                if (IsFromDataManage)
                {
                    return "保存";
                }
                else
                {
                    return "保存并返回";
                }
            }
        }

        protected string btnBackText
        {
            get
            {
                if (IsFromDataManage)
                {
                    return "关闭";
                }
                else
                {
                    return "返回";
                }
            }
        }

        /// <summary>
        /// 档案ID
        /// </summary>
        protected string ArchiveID
        {
            get
            {
                if (Request.QueryString["archiveId"] != null)
                {
                    ViewState["archiveId"] = Request.QueryString["archiveId"];
                }
                if (ViewState["archiveId"] != null)
                {
                    return ViewState["archiveId"].ToString();
                }
                else
                {
                    ViewState["archiveId"] = Guid.NewGuid().ToString();
                    return ViewState["archiveId"].ToString();
                }
            }
        }

        /// <summary>
        /// 当前页的档案类型-派生类需要指定
        /// </summary>
        protected ArchiveTableEnum CurrentArchiveKind
        {
            get
            {
                if (_currArchiveKind == 0)
                {
                    if (Request.QueryString["archiveType"] != null)
                    {
                        //int baseTypeId = IArchiveMenuService.GetArchiveBaseIDById(int.Parse(Request.QueryString["archiveType"]));
                        int baseTypeId = int.Parse(Request.QueryString["archiveType"]);
                        //根据档案类型查询基类型
                        _currArchiveKind = (ArchiveTableEnum)baseTypeId;
                    }
                    else
                    {
                        throw new Exception("档案类型不能为空");
                    }
                }
                return _currArchiveKind;
            }
        }

        private string _attachDBString;
        protected string AttachDBString
        {
            get
            {
                if (!string.IsNullOrEmpty(_attachDBString))
                {
                    return _attachDBString;
                }
                IArchiveMenuService ArchiveMenuService = UContainer.Resolve<IArchiveMenuService>();
                //根据类型，查询基类型
                ArchiveTableEnum baseType = (ArchiveTableEnum)ArchiveMenuService.GetArchiveBaseIDById((int)CurrentArchiveKind);
                switch (baseType)
                {
                    case ArchiveTableEnum.Document:
                        _attachDBString = AttachDbHelperString.DocumentFile;
                        break;
                    case ArchiveTableEnum.Account:
                        _attachDBString = AttachDbHelperString.AccountFile;
                        break;
                    case ArchiveTableEnum.AudioVedio:
                        _attachDBString = AttachDbHelperString.Audiofile;
                        break;
                    case ArchiveTableEnum.BulidProject:
                        _attachDBString = AttachDbHelperString.BuildProjectFile;
                        break;
                    case ArchiveTableEnum.CDKind:
                        _attachDBString = AttachDbHelperString.CDKind;
                        break;
                    case ArchiveTableEnum.Equipment:
                        _attachDBString = AttachDbHelperString.Equipment;
                        break;
                    case ArchiveTableEnum.OliSearch:
                        _attachDBString = AttachDbHelperString.Oilsearch;
                        break;
                    case ArchiveTableEnum.RealKind:
                        _attachDBString = AttachDbHelperString.RealKind;
                        break;
                    case ArchiveTableEnum.Technology:
                        _attachDBString = AttachDbHelperString.Technology;
                        break;
                }
                return _attachDBString;
            }
        }

        protected void GetPrivilege(ArchiveTableEnum archiveTypeTableEnum, string dataId, string statusId)
        {
            ///时间：2012年11月26日 00:41:40
            ///默认如果不传入这个参数，则表示是走审核流程，如果传入，并且 needcheck=1，则表示是其他界面链接进入，只供查看
            if (!string.IsNullOrEmpty(Request.QueryString["needcheck"]) || Request.QueryString["needcheck"] == "1")
            {
                ///查看则无返回按钮
                showReturn = false;
                return;
            }
            else if (!string.IsNullOrEmpty(Request.QueryString["op"]) && Request.QueryString["op"] == "scan")
            {
                ///查看则无返回按钮
                showReturn = false;
                return;
            }
            ///added at 2013年1月8日 22:55:41
            ///不需要执行 GetPrivilege 方法
            else
            {
                canEdit = true;
                canOperate = true;
                return;
            }

        }

        /// <summary>
        /// 返回
        /// </summary>
        protected void GoBack()
        {
            switch (Request.QueryString["currentstatusname"])
            {
                case "上交审核中":
                    this.Response.Redirect("ArchiveHandinList.aspx?savecookie=1&archiveType=" + (int)CurrentArchiveKind);
                    break;
                case "接收审核中":
                    this.Response.Redirect("ArchiveReceiveList.aspx?savecookie=1&archiveType=" + (int)CurrentArchiveKind);
                    break;
                case "办结审核中":
                default:
                    this.Response.Redirect("ArchiveDoneList.aspx?savecookie=1&archiveType=" + (int)CurrentArchiveKind);
                    break;
            }

        }

        /// <summary>
        /// 操作标志位
        /// scan:        //情况之一：档案借阅-领导审核[能浏览附件，不能操作]
        /// readonly:    //来自档案借阅-浏览档案[不能浏览附件 不能操作]
        /// fromdatamgr: //来自数据管理-浏览[能浏览附件，不能操作]
        /// </summary>
        protected string OP
        {
            get
            {
                return Request.QueryString["op"] ?? "scan";
            }
        }

        protected void gv_AttachMent_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRow row = ((DataRowView)e.Row.DataItem).Row;
                e.Row.Cells[1].Text = row["attachmentname"].ToString();
                ///浏览
                switch (OP.ToLower())
                {
                    case "scan":
                    case "fromdatamgr":
                    default:
                        e.Row.Cells[1].Text = string.Format("<a target='_blank' href='../../UserControls/AttachmentDownload.aspx?AttachmentID={0}&AttachmentDBHelperString={1}'>{2}</a>",
                        row["attachmentid"], AttachDBString, row["attachmentname"].ToString());
                        break;
                    case "readonly":
                        break;
                }


            }
        }
    }
}